<template>
    <div>
        <!--搜索框-->
        <div class="search-form">
            <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                <el-form-item label="部门名">
                    <el-input v-model="searchForm.name" placeholder="请输入部门名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="searchSubmit">查询</el-button>
                </el-form-item>
            </el-form>
        </div>

        <!--头部工具栏-->
        <div class="header-toolbox">
            <el-button type="primary" size="small" @click="addOrUpdateUser">增加</el-button>
        </div>

        <!--     :load="load"   表格数据-->
        <div>
            <el-table
                    :data="tableData"
                    style="width: 100%"
                    row-key="id"
                    border
                    lazy
                    height="525px"

                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                        label="部门名">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="描述">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.remark }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="地址">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.address }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="创建时间">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.createtime }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="150px">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <el-dialog :title="title" :visible.sync="deptFormVisible" @close="closeUserForm">
            <el-form :model="deptForm" :rules="deptFormRules" ref="deptFormRef" label-width="80px"
                     label-position="left">
                <el-input v-model="deptForm.id" v-show="false"></el-input>
                <el-form-item label="部门名" prop="name">
                    <el-input v-model="deptForm.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="上级部门" prop="pid" >
                    <el-select v-model="deptForm.pid" placeholder="请选择部门" style="width: 100%;">
                        <el-option :label="dept.name" :value="dept.id" :key="index"
                                   v-for="(dept, index) in depts"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="描述" prop="remark">
                    <el-input v-model="deptForm.remark" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="deptForm.address" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" align="center">
                <el-button @click="deptFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitUser">确 定</el-button>
            </div>
        </el-dialog>

    </div>

</template>

<script>
import {
    addAndUpdateDeptAPI,
    addOrUpdateUserAPI,
    batchDeleteUserAPI, changeAvailableAPI, deleteByIdDeptAPI, getCurrentPageDeptInfoAPI,
    getDeptsAPI, getDeptsListAPI, getFullDeptInfoAPI,
    getFullUserInfoAPI,
    getRolesAPI,
    searchPageInfoAPI,
    updateUserStateAPI
} from '@/request/api'
import {listToTree} from "@/util/treeUtil";

export default {
    name: "DeptView",
    data() {
        return {
            title: '',
            total: 100,
            deptFormVisible: false,
            searchForm: {
            },
            depts:[],
            tableData: [],
            multipleSelection: [],
            deptForm: {},
            deptFormRules: {
                name: [
                    {required: true, message: '请填写部门名', trigger: 'blur'}
                ],
                remark: [
                    {required: true, message: '请填写描述', trigger: 'blur'}
                ],
                address: [
                    {required: true, message: '请选择地址', trigger: 'blur'}
                ],
            }
        }
    },
    methods: {
        searchSubmit() {
            this.list()
        },
        resetSearchForm() {
            this.searchForm = {
            }
        },
        // 用户编辑
        handleEdit(index, row) {
            this.title = '编辑部门';
            this.deptFormVisible = true;
            getFullDeptInfoAPI(row.id).then(response => {
                if (response.code == 200) {
                    this.deptForm = response.data
                }
            }).catch(error => {
                this.$message.error('回显获取请求失败')
            })
        },
        // 用户删除
        handleDelete(index, row) {
            this.$confirm('确认执行删除操作么, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteByIdDeptAPI(row.id).then(response => {
                    if (response.code == 200) {
                        this.$message.success({
                            type: 'success',
                            message: response.msg,
                            onClose: () => {
                                this.searchForm = {
                                    currentPage: 1,
                                    pageSize: 5,
                                }
                                this.list()
                            },
                        })
                        return
                    }
                    this.$message.error(response.msg)
                }).catch(error => {
                    this.$message.error('删除操作错误')
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        list() {
            getCurrentPageDeptInfoAPI(this.searchForm).then(response => {
                // 进行判断 注入值
                if (response.code == 200) {
                    this.tableData = listToTree(response.data.data);
                    this.total = response.data.count;
                    return;
                }
                this.$message.error(response.msg)
            }).catch(error => {
                this.$message.error("操作失败")
            })
        },
        // 打开用户用户表单
        addOrUpdateUser() {
            this.title = '添加部门'
            this.deptFormVisible = !this.deptFormVisible;
        },
        // 把添加用户和更新用户请求封成一个方法
        addOrUpdateDeptBody() {
            addAndUpdateDeptAPI(this.deptForm).then(response => {
                if (response.code == 200) {
                    this.$message.success({
                        type: 'success',
                        message: response.msg,
                        onClose: () => {
                            this.list()
                            this.deptFormVisible = false;
                            this.deptForm = {}
                        }
                    })
                    return;
                }
                this.$message.error(response.msg)
            }).catch(error => {
                this.$message.error("用户操作失败")
            })
        },
        // 用户表单增加或修改
        submitUser() {
            this.addOrUpdateDeptBody()
        },
        // 清除意外关闭表单的数据
        closeUserForm() {
            this.deptForm = {}
            this.$refs.deptFormRef.clearValidate()
        },
        renderSelect(){
            getDeptsListAPI().then(response => {
                this.depts = response.data;
                this.depts.push({name:' ', pid:0, id: 0})
            })
        }
    },
    created() {
        this.list()
        this.renderSelect()
    }
}
</script>

<style scoped>
.demo-form-inline {
    float: left;
    padding: 15px 0px 5px 10px;

}

.header-toolbox {
    width: 180px;
    padding-bottom: 10px;
}

.foot-pageInfo {
    float: left;
}
</style>
